<?php

// @todo: determine required php version and update ddev config
$majorVersionNumber = (int)$argv[1];

if ($majorVersionNumber < 11) {
    echo "Please set a version greater then '10'\n";
    exit;
}

chdir(__DIR__ . '/../');
$json = json_decode(file_get_contents('composer.json'), true, 512, JSON_THROW_ON_ERROR);

$packageList = [];
foreach ($json['require'] as $package => $constraint) {
    if (str_starts_with($package, 'typo3/cms-')) {
        $packageList[] = $package . ':^' . $majorVersionNumber;
    }
}

$composer = str_replace(PHP_EOL, '', shell_exec('which composer'));
if (empty($composer)) {
    echo "Composer binary not found ...\n";
    exit;
}

$command = "$composer require " . implode(' ', $packageList) . " --no-update";
shell_exec($command);
unlink('composer.lock');
shell_exec($composer . ' update -W');
